import { NestFactory } from '@nestjs/core'
import { VersioningType } from '@nestjs/common'
import { AppModule } from './app.module'
import { initDoc } from './doc'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
// import { AllExceptionsFilter } from './common/exceptions/base.exception.filter'
import { TransformInterceptor } from './common/interceptors/transform.interceptor'
import { NestExpressApplication } from '@nestjs/platform-express'
import * as cors from 'cors' //引入
import { join } from 'path'
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule)
  //初始化doc
  initDoc(app)

  // 版本控制
  app.enableVersioning({
    defaultVersion: '1',
    type: VersioningType.URI,
  })
  // 全局前缀
  app.setGlobalPrefix('/api')

  //全局过滤器
  // app.useGlobalFilters(new AllExceptionsFilter())
  app.useGlobalInterceptors(new TransformInterceptor())
  app.use(cors()) //挂载
  //swagger接口文档文档
  const swaggerOptions = new DocumentBuilder()
    .setTitle('Demo的接口文档')
    .setDescription('第一次尝试使用swagger')
    .setVersion('1.0.0')
    .build()
  const swaggerDocument = SwaggerModule.createDocument(app, swaggerOptions)
  SwaggerModule.setup('/api-docs', app, swaggerDocument)
  //静态文件访问
  app.useStaticAssets(join(__dirname, 'images'), {
    prefix: '/images/',
  })
  await app.listen(3001)
}
bootstrap()
